using System;
using System.Collections.Generic;
using System.Text;
using System.Media;
using System.Threading;

namespace AleaPlayer
{
    class TSoundPlayerWithItem<T>: IEquatable<TSoundPlayerWithItem<T>>
    {
        public delegate void FinishedPlayingDelegate(T item);

        SoundPlayer _soundPlayer = null;
        T _item = default(T);
        FinishedPlayingDelegate _finishedPlayingDelegate = null;


        public TSoundPlayerWithItem(SoundPlayer soundPlayer, T item, FinishedPlayingDelegate finishedPlayingDelegate)
        {
            _soundPlayer = soundPlayer;
            _item = item;
            _finishedPlayingDelegate = finishedPlayingDelegate;
        }

        public SoundPlayer oSoundPlayer
        {
            get
            {
                return _soundPlayer;
            }
        }

        public T oItem
        {
            get
            {
                return _item;
            }
        }

        #region IEquatable<TSoundPlayerWithItem<T>> Members

        bool IEquatable<TSoundPlayerWithItem<T>>.Equals(TSoundPlayerWithItem<T> other)
        {
            return other._item.Equals(_item); 
        }

        Thread _Thread = null;

        public void PlaySound()
        {
             _Thread = new Thread(new ThreadStart(DoPlaySound));
            _Thread.Start();
        }

        private void DoPlaySound()
        {
            _soundPlayer.PlaySync();
            _Thread = null;
            if (_finishedPlayingDelegate != null)
                _finishedPlayingDelegate(_item);
        }

        public void StopSound()
        {
            //_soundPlayer.Stop();
        }

        public bool IsPlaying
        {
            get
            {
                return _Thread != null;
            }
        }

        #endregion
    }
}
